package main

import (
	"crypto/tls"
	"crypto/x509"
	"io/ioutil"
	"log"
	"net/http"
)

func clientStart() {
	var (
		resp   *http.Response
		err    error
		body   []byte
		tr     *http.Transport
		client *http.Client
		pool   *x509.CertPool
		crt    []byte
		cliCrt tls.Certificate
	)
	pool = x509.NewCertPool()
	// 读取证书
	crt, err = ioutil.ReadFile("/Users/blue7/work/go-workplace/go-https/cert/rootCA.crt")
	if err != nil {
		log.Fatalln("读取证书错误", err)
	}
	pool.AppendCertsFromPEM(crt)
	// 加载X509 client证书和秘钥
	cliCrt, err = tls.LoadX509KeyPair("/Users/blue7/work/go-workplace/go-https/cert/client.crt", "/Users/blue7/work/go-workplace/go-https/cert/client.key")
	if err != nil {
		panic(err)
	}

	tr = &http.Transport{
		TLSClientConfig: &tls.Config{
			// InsecureSkipVerify: true,
			RootCAs:      pool,
			Certificates: []tls.Certificate{cliCrt},
		},
		// DisableCompression: true,
	}

	client = &http.Client{Transport: tr}

	//resp, err = client.Get("https://127.0.0.1:9898/test")
	resp, err = client.Get("https://localhost:9898/test")

	if err != nil {
		log.Fatalln("接口请求失败", err)
	}

	defer resp.Body.Close()

	body, err = ioutil.ReadAll(resp.Body)

	log.Println("请求结果", string(body))
}
